﻿#include "page.h"
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "mainPage.h"
#include "weather.h"
#include "http_api.h"
#include "cjson.h"
lv_obj_t *cityName;
lv_obj_t *tempNow;
lv_obj_t *weatherNow;
lv_obj_t *updateTime;
/**
 * @brief
 * {"results":
 *  [   {"location":
 *          {"id":"WTMKQ069CCJ7","name":"Hangzhou","country":"CN","path":"Hangzhou,Hangzhou,Zhejiang,China","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},
 *      "now":{"text":"Cloudy","code":"4","temperature":"17"},
 *          "last_update":"2021-11-19T10:51:58+08:00"}
 * ]
 * }
 *
 * @param data
 */
void weather_api_deal(char *data)
{
    cJSON *parse = cJSON_Parse(data);
    cJSON *reslut = cJSON_GetObjectItem(parse, "results");
    cJSON *arr = cJSON_GetArrayItem(reslut, 0);
    cJSON *now = cJSON_GetObjectItem(arr, "now");
    cJSON *text = cJSON_GetObjectItem(now, "text");
    cJSON *temperature = cJSON_GetObjectItem(now, "temperature");
    cJSON *last = cJSON_GetObjectItem(arr, "last_update");
    lv_label_set_text_fmt(tempNow, "temperature: %s C", temperature->valuestring);
    lv_label_set_text_fmt(weatherNow, "weather now: %s", text->valuestring);
    lv_label_set_text_fmt(updateTime, "update time: %s", last->valuestring);
    // lv_label_set_text(tempNow,  temperature->valuestring);
    // lv_label_set_text(weatherNow, text->valuestring);
    // lv_label_set_text(updateTime,  last->valuestring);
    cJSON_Delete(parse);
}
void show_weather_page()
{
    lv_group_remove_all_objs(group);
    http_api_get("api.seniverse.com", "/v3/weather/now.json?key=SK__R6hWJW7vsNfGP&location=hangzhou&language=en&unit=c", weather_api_deal);

    lv_scr_load(weatherPage.page);
    push_page(&weatherPage);
}


void page_weather_init()
{
    weatherPage.page = lv_obj_create(0);
    cityName = lv_label_create(weatherPage.page);
    lv_label_set_text(cityName, "city:HanZhou");
    lv_obj_set_pos(cityName, 20, 20);
    // lv_obj_set_size(cityName,320,40);
    

    weatherNow = lv_label_create(weatherPage.page);
    lv_label_set_text(weatherNow, "HanZhou");
    lv_obj_set_pos(weatherNow, 20, 80);
    // lv_obj_set_size(weatherNow,320,40);


    tempNow = lv_label_create(weatherPage.page);
    lv_label_set_text(tempNow, "HanZhou");
    lv_obj_set_pos(tempNow, 20, 140);
    // lv_obj_set_size(tempNow,320,40);


    updateTime = lv_label_create(weatherPage.page);
    lv_label_set_text(updateTime, "HanZhou");
    lv_obj_set_pos(updateTime, 20, 200);
    // lv_obj_set_size(updateTime,320,40);


    weatherPage.show = show_weather_page;
}
